package com.ease.util.logging {

   import com.ease.util.MixedUtil;

   import flash.external.ExternalInterface;
   import flash.system.Capabilities;

   import mx.core.mx_internal;
   import mx.logging.ILogger;
   import mx.logging.LogEvent;
   import mx.logging.LogEventLevel;
   import mx.logging.targets.LineFormattedTarget;

   use namespace mx_internal;

   /**
    * A logging target for Firefox's Firebug console.
    */
   public class FirebugTarget extends LineFormattedTargetEx {
      private var _firebugEnabled:Boolean;

      public function FirebugTarget() {
         var isBrowser:Boolean = (Capabilities.playerType == "ActiveX" ||
            Capabilities.playerType == "PlugIn");
         if (isBrowser && ExternalInterface.available) {
            // check if firebug is installed and enabled
            _firebugEnabled = ExternalInterface.call(
               "function(){ " +
               "   return typeof window.console == 'object' && " +
               "          typeof console.firebug == 'string'" +
               "}");
         }
      }

      public override function logEvent(event:LogEvent):void {
         if (!_firebugEnabled) {
            return;
         }
         super.logEvent(event);
      }

      override protected function logMessage(message:String, level:int):void {
         // compute the method name to be called on the console.
         var levelStr:String;
         if (level == LogEventLevel.FATAL) {
            levelStr = LogEvent.getLevelString(LogEventLevel.ERROR);
         } else {
            levelStr = LogEvent.getLevelString(level);
         }
         // call the logging method on the console.
         ExternalInterface.call("console." + levelStr.toLocaleLowerCase(),
            message);
      }

   }
}
